package com.zhongwang.mms.module.basic.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.MMaterialDictionary;
import com.zhongwang.mms.module.basic.model.MMaterialDictionaryModel;
import com.zhongwang.mms.module.basic.service.MMaterialDictionaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 添加原料数据字典 控制层
 * @Author: China.wsg
 * @Date: 2020/03/16 11:23
 */
@Controller
@RequestMapping("/basic/materialdictionary")
public class MMaterialDictionaryController extends BaseController {

    @Autowired
    private MMaterialDictionaryService mMaterialDictionaryService;

    /**
     * 添加原料数据字典 页面
     *
     * @return 页面路径
     */
    @GetMapping("/page")
    @RequiresPermissions({"basic:rawmatinfo:view"})
    public String page() {
        return "basic/materialdictionary/list";
    }

    /**
     * 根据原料分类取得其余数据字典信息
     *
     * @param mdTypeNum 类别编号
     * @return
     */
    @ResponseBody
    @GetMapping("/getMaterialCode")
    public JsonResult getMaterialCode(Integer mdTypeNum) {

        String materialCode = mMaterialDictionaryService.getMaterialCode(mdTypeNum);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(materialCode);
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 原料数据字典列表
     *
     * @param materialDictionary 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"basic:rawmatinfo:view"})
    public JsonResult list(MMaterialDictionary materialDictionary) {
        startPage();
        List<MMaterialDictionaryModel> list = mMaterialDictionaryService.findMaterialDictionaryByQuery(materialDictionary);
        return jsonResult(list);
    }

    /**
     * 添加页面
     */
    @GetMapping("/create")
    @RequiresPermissions({"basic:rawmatinfo:edit"})
    public String create() {
        return "basic/materialdictionary/create";
    }

    /**
     * 保存原料数据字典
     *
     * @param materialDictionary 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"basic:rawmatinfo:edit"})
    public JsonResult save(MMaterialDictionary materialDictionary) {
        JsonResult jsonResult = new JsonResult();
        String msg = mMaterialDictionaryService.saveMaterialDictionary(materialDictionary);
        if (msg.equals("")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 删除原料数据字典
     *
     * @param mMaterialDictionary 被删除数据
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"basic:rawmatinfo:edit"})
    public JsonResult del(MMaterialDictionary mMaterialDictionary) {
        JsonResult jsonResult = new JsonResult();
        String msg = mMaterialDictionaryService.delMaterialDictionary(mMaterialDictionary);
        if (msg.equals("")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 取得数据字典被原料引用次数
     *
     * @param mdId 字典主键
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/usedcount")
    public JsonResult usedCount(Integer mdId) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        jsonResult.setData(mMaterialDictionaryService.usedCount(mdId));
        return jsonResult;
    }

    /**
     * 修改原料数据字典
     *
     * @param materialDictionary 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"basic:rawmatinfo:edit"})
    public JsonResult update(MMaterialDictionary materialDictionary) {
        JsonResult jsonResult = new JsonResult();
        String msg = mMaterialDictionaryService.updateMaterialDictionary(materialDictionary);
        if (msg.equals("")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }


}
